package com.gopay.bis.ipay.ebank.operation;

import java.security.PrivateKey;
import java.security.PublicKey;


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.ipay.ebank.rsa.CryptoUtil;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ipay.ebank.IpayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;

/**
 * 民生i支付签名验签实现
 * 
 * @IpayCertHelper.java
 * @author lilili
 * @2016年7月8日 下午3:38:10 www.gopay.com.cn Inc.All rights reserved.
 */
public class IpayCertHelper implements ICertHelper {

	private static Log logger = LogFactory.getLog(IpayCertHelper.class);
	private String pfxFilePath;
	private String cerFilePath;

	@Override
	public String sign(BaseMessage msg) throws GopayException {
		String signData = "";
		IpayMessage message = (IpayMessage) msg;
		try {
			PrivateKey hzfPriKey = CryptoUtil.getRSAPrivateKeyByFileSuffix(pfxFilePath, "pem", null, "RSA");
			byte[] base64SingDataBytes = Base64.encodeBase64(
					CryptoUtil.digitalSign(message.getPlainMessage().getBytes("UTF-8"), hzfPriKey, "SHA1WithRSA"));
			signData = new String(base64SingDataBytes, "UTF-8");
		} catch (Exception e) {
			BankLogUtils.printB2CSignExceptionLog(msg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return signData;
	}

	
	public static void main(String[] args) {
		String signData = "";
		String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<message>"+
"	<head>"+
"		<version>1.0.0</version>"+
"		<msgType>01</msgType>"+
"		<reqDate>20160909163635</reqDate>"+
"	</head>"+
"	<body>"+
"		<reqMsgId>20160909163000</reqMsgId>"+
"		<bankCardNo>6225768313000000</bankCardNo>"+
"		<accountName>林尚青</accountName>"+
"		<bankCardType>02</bankCardType>"+
"		<certificateType>ZR01</certificateType>"+
"		<certificateNo>350321198801010000</certificateNo>"+
"		<mobilePhone>15959200000</mobilePhone>"+
"		<valid>0120</valid>"+
"		<cvn2>001</cvn2>"+
"		<userId>00020160909103049</userId>"+
"	</body>"+
"</message>";
		try {
			//String path = "F:\\key\\expay_pkcs8_rsa_private_key_2048.pem";
			String priPath = "F:\\key\\pkcs8_rsa_private_key_2048.pem";
			String pubPath = "F:\\key\\rsa_public_key_2048.pem";
			PrivateKey hzfPriKey = CryptoUtil.getRSAPrivateKeyByFileSuffix(priPath, "pem", null, "RSA");
			byte[] base64SingDataBytes = Base64.encodeBase64(
					CryptoUtil.digitalSign(str.getBytes("UTF-8"), hzfPriKey, "SHA1WithRSA"));
			signData = new String(base64SingDataBytes, "UTF-8");
			System.out.println(signData);
			
			PublicKey hzfPubKey = CryptoUtil.getRSAPublicKeyByFileSuffix(pubPath, "pem", "RSA");
			byte[] signBytes = Base64.decodeBase64(signData.getBytes("UTF-8"));
			// 使用商户公钥验证签名
			boolean verifySign = CryptoUtil.verifyDigitalSign(str.getBytes("UTF-8"), signBytes, hzfPubKey,
					"SHA1WithRSA");
			System.out.println(verifySign);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Object verify(BaseMessage msg) throws GopayException {
		boolean verifySign = false;
		try {
			IpayMessage message = (IpayMessage) msg;
			PublicKey hzfPubKey = CryptoUtil.getRSAPublicKeyByFileSuffix(cerFilePath, "pem", "RSA");
			byte[] signBytes = Base64.decodeBase64(message.getSignMessage().getBytes("UTF-8"));
			// 使用商户公钥验证签名
			verifySign = CryptoUtil.verifyDigitalSign(message.getPlainMessage().getBytes("UTF-8"), signBytes, hzfPubKey,
					"SHA1WithRSA");
					
					
			logger.error("ipay bank verify plain---:"+message.getPlainMessage());
			logger.error("ipay bank veirfy sign---:"+message.getSignMessage());
			logger.error("ipay bank verify result---:"+verifySign);
			
		} catch (Exception e) {
			BankLogUtils.printB2CVerifyExceptionLog(msg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
		}

		return verifySign;
	}
	

	public String getPfxFilePath() {
		return pfxFilePath;
	}

	public void setPfxFilePath(String pfxFilePath) {
		this.pfxFilePath = pfxFilePath;
	}

	public String getCerFilePath() {
		return cerFilePath;
	}

	public void setCerFilePath(String cerFilePath) {
		this.cerFilePath = cerFilePath;
	}

}
